package com.seeyon.ctp.rest.resources;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.alibaba.fastjson.JSONArray;
import fy.config.FyConfig;
import fy.util.DBUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
//import com.seeyon.apps.zhongh.controller.HttpUtil;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.principal.PrincipalManager;
import com.seeyon.ctp.services.security.ServiceManager;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.annotation.RestInterfaceAnnotation;

/**
 * @author gnb577
 */
@Path("CertificateLogin")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CertificateLogin extends BaseResource{
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    //@RestInterfaceAnnotation
    public Response CertificateLogin(Map data){

        Object loginname = data.get("loginname");
        Object password = data.get("password");
        System.out.println("获取参数："+loginname+","+password);
        if(loginname==null||password==null||loginname.toString().equals("")||password.toString().equals("")){
            return ok("{\"code\":\"202\",\"message\":\"账号或密码为空\"}");
        }
        PrincipalManager principalManager=(PrincipalManager) AppContext.getBean("principalManager");
        //验证账号密码
        boolean b=principalManager.authenticate(loginname.toString(),password.toString());
        if(b==true){
            OrgManager om=(OrgManager)AppContext.getBean("orgManager");
            try {
                V3xOrgMember m = om.getMemberByLoginName(loginname.toString());
                return ok("{\"code\":\"200\",\"message\":\"验证成功\",\"phone\":\""+m.getTelNumber()+"\",\"tel\":\""+m.getOfficeNum()+"\"}");
            } catch (Exception e) {
                return ok("{\"code\":\"201\",\"message\":\""+e.getMessage()+"\",\"phone\":\"\",\"tel\":\"\"}");
            }

        }else{
            return ok("{\"code\":\"201\",\"message\":\"账号密码错误\",\"phone\":\"\",\"tel\":\"\"}");
        }
    }
}
